package com.iflysse.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.iflysse.dao.ContentInfoDao;
import com.iflysse.pojo.ContentInfo;
import com.iflysse.utils.MySessionFactory;
import com.iflysse.utils.PagerBean;

public class ContentInfoService {
	
public List<ContentInfo> findByContentAll(){
	List<ContentInfo> info=null;
	SqlSession session=null;
	try{
		session=sf.openSession();
		ContentInfoDao dao=session.getMapper(ContentInfoDao.class);
		info=dao.findContentAll();
	}finally{
		session.close();
	}
	 return info;
}

	private SqlSessionFactory sf;

	public ContentInfoService() {
		sf = MySessionFactory.getSessionFactory();
	}

	public ContentInfo findByContentId(int id) {
		ContentInfo info = null;
		SqlSession session = null;
		try {
			session = sf.openSession();
			ContentInfoDao dao = session.getMapper(ContentInfoDao.class);
			info = dao.findByContentId(id);
		} finally {
			session.close();
		}
		return info;
	}

	public void insertContent(ContentInfo contentInfo) {
		SqlSession session = null;
		try {
			session = sf.openSession();
			ContentInfoDao dao = session.getMapper(ContentInfoDao.class);
			dao.insertContent(contentInfo);
			session.commit();
		} finally {
			session.close();
		}

	}

	public void updateContent(ContentInfo contentInfo) {
		SqlSession session = null;
		try {
			session = sf.openSession();
			ContentInfoDao dao = session.getMapper(ContentInfoDao.class);
			dao.updateContent(contentInfo);
			session.commit();
		} finally {
			session.close();
		}

	}

	public void delByContentId(int id) {
		SqlSession session = null;
		try {
			session = sf.openSession();
			ContentInfoDao dao = session.getMapper(ContentInfoDao.class);
			dao.delByContentId(id);
			session.commit();
		} finally {
			session.close();
		}

	}
	public long findContentTotal()
	{
		SqlSession session=null;
		long  total=0;
		try{
			session=sf.openSession();
			ContentInfoDao dao=session.getMapper(ContentInfoDao.class);
			total=dao.findContentTotal();
		}finally{
			session.close();
		}
		return total;
	}
	public PagerBean<ContentInfo> findContentPager(PagerBean<ContentInfo> pager){
		SqlSession session=null;
	
		try{
			session=sf.openSession();
			ContentInfoDao dao=session.getMapper(ContentInfoDao.class);
			pager.setRows(dao.findContentPager(pager));
		}finally{
			session.close();
		}
		return pager;
	}

	/**
	*
	* @author: chen
	* @createTime: 2014年11月28日 下午3:37:01
	* @history: void
	*/
	public List<ContentInfo> findByContentByName(String columnName) {
		SqlSession session = null;
		List<ContentInfo> content = null;
		try {
			session = sf.openSession();
			ContentInfoDao dao = session.getMapper(ContentInfoDao.class);
			content = dao.findByContentByColumnName(columnName);
			session.close();
		} finally {
			session.close();
		}
		return content;
	}

}
